﻿using Rubyer;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using static SOCON.API.Utility;

namespace Ozone.Device
{
    /// <summary>
    /// SOPA气体置换式移液器
    /// </summary>
    public class devSOPASocaXY
    {
        delegate void SetValueHandler(string value);
        //是否打开串口
        public static bool isOpen = false;
        //扫描到节点值
        int selectedNodeID = 2;
        //停止扫描
        bool stopScan = false;
        //正在扫描
        bool isScaning = false;
        //节点List （注：连接多个液枪 目前应用场景只连接一个液枪）
        List<int> socaList = new List<int>();
        private SOCON.API.SCDevice device = new SOCON.API.SCDevice();

        string devPort = "";
        /// <summary>
        /// 构造函数 
        /// </summary>
        public devSOPASocaXY()
        {
            // devPort = sPort;

        }
        /// <summary>
        /// 打开COM口扫描节点
        /// </summary>
        /// <param name="COM"></param>
        public void Open()
        {
            if (isOpen == true && selectedNodeID != 0)
            {
                //已初始化
            }
            else
            {

                //启动日志
                SOCON.Utility.MessageService.StartLog();

                //设置连接类型
                device.ConnectType = SOCON.API.Utility.e_ConnectType.CONN_RS485;
                device.RegisteDeviceType(2, SOCON.API.Utility.DeviceTypeEnum.ADP);
                int deviceIndex = int.Parse("7");
                isOpen = device.OpenPort(deviceIndex, 115200);
            }

        }




        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public bool Init()
        {
            bool isInitZ = true;

            return isInitZ;
        }


        public bool ConnectionStatus()
        {
            return isOpen;
        }


        /// <summary>
        /// 初始化ADP (吸液 吐液等操作需要使用到ADP相关功能需要先初始化ADP)
        /// </summary>
        /// <returns></returns>
        public bool InitADP_Click()
        {
            bool isInitADP = false;
            try
            {
                if (!isOpen || selectedNodeID == 0) return isInitADP;

                //执行ADP初始化
                string ret = device.InitSOPA(selectedNodeID);




                if (ret != "")
                {
                    SOCON.Utility.MessageService.WriteLogToFile(SOCON.Utility.MessageService.LOGLEVEL.错误, $"{selectedNodeID} Err:{ret}");
                    MessageBox.Show(ret);
                }
                //执行状态查询
                ret = device.WaitActionDone(selectedNodeID); //等待动作完成
                if (ret != "")
                {
                    SOCON.Utility.MessageService.WriteLogToFile(SOCON.Utility.MessageService.LOGLEVEL.错误, $"{selectedNodeID} Err:{ret}");
                    MessageBox.Show(ret);
                }
                else
                {
                    isInitADP = true;
                }
            }
            catch (Exception ex)
            {
            }

            return isInitADP;
        }




        /// <summary>
        /// 退吸头
        /// </summary>
        public void DropDitt()
        {

            string ret = device.DropDiti(2);
            if (ret != "")
            {
                SOCON.Utility.MessageService.WriteLogToFile(SOCON.Utility.MessageService.LOGLEVEL.错误, $"{selectedNodeID} Err:{ret}");
                MessageBox.Show(ret);
            }

            ret = device.WaitActionDone(selectedNodeID); //等待动作完成
            if (ret != "")
            {
                SOCON.Utility.MessageService.WriteLogToFile(SOCON.Utility.MessageService.LOGLEVEL.错误, $"{selectedNodeID} Err:{ret}");
                MessageBox.Show(ret);
            }
        }


        /// <summary>
        /// 吸液
        /// </summary>
        /// <param name="aspirationVol">吸液量（单位：ul）</param>
        /// <param name="speed"> 吸液速度（单位：0.1ul/s）</param>
        public void Aspirate(float aspirationVol, float speed)
        {
            if (!isOpen || selectedNodeID == 0) return;


            string ret = device.Aspirate(selectedNodeID, aspirationVol, speed);
            if (ret != "")
            {
                SOCON.Utility.MessageService.WriteLogToFile(SOCON.Utility.MessageService.LOGLEVEL.错误, $"{selectedNodeID} Err:{ret}");
                MessageBox.Show(ret);
            }

            ret = device.WaitActionDone(selectedNodeID, 4000); //等待动作完成
            if (ret != "")
            {
                SOCON.Utility.MessageService.WriteLogToFile(SOCON.Utility.MessageService.LOGLEVEL.错误, $"{selectedNodeID} Err:{ret}");
                MessageBox.Show(ret);
            }
        }


        /// <summary>
        /// 吐液
        /// </summary>
        /// <param name="nbDispenseVol"></param>

        public void Dispense(float dispenseVol, float speed)
        {
            if (!isOpen || selectedNodeID == 0) return;
            string ret = device.Dispense(selectedNodeID, dispenseVol, speed);
            if (ret != "")
            {
                SOCON.Utility.MessageService.WriteLogToFile(SOCON.Utility.MessageService.LOGLEVEL.错误, $"{selectedNodeID} Err:{ret}");
                MessageBox.Show(ret);
            }

            ret = device.WaitActionDone(selectedNodeID); //等待动作完成
            if (ret != "")
            {
                SOCON.Utility.MessageService.WriteLogToFile(SOCON.Utility.MessageService.LOGLEVEL.错误, $"{selectedNodeID} Err:{ret}");
                MessageBox.Show(ret);
            }
        }


    }
}
